<?php
/**
 * Copyright (C) 2013 Pablo Sepúlveda P. <psep.pentauc@gmail.com>
 * 
 * This file is part of gAbstract.
 * WarpIT is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * gAbstract is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with gAbstract.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Clase estática de componentes Utils que sirve para manipular ficheros.
 * 
 * @author psep
 * @version 1.0
 * @package gAbstract
 * @copyright Copyright (c) 2013 Pablo Sepúlveda P.
 * @license GNU GPLv3 or later
 * @link http://www.psep.cl
 */ 
final class FileUtils {
	
	/**
	 * This method mimics a recursive copy in bash script
	 *
	 * Following syntax are recognized :	 
	 * 
	 * @param string $source original_folder
	 * @param string $target copy_folder
	 */
	public static function fullRecursiveCopy($source, $target){
		if(is_dir($source)) {
			@mkdir($target);
			$d = dir($source);
			
			while(FALSE !== ($entry = $d->read())){
				if($entry == '.' || $entry == '..'){
					continue;
				}
				
				$Entry = $source . '/' . $entry;
				
				if(is_dir($Entry)){
					static::fullRecursiveCopy($Entry, $target.'/'.$entry);
					continue;
				}
				
				copy($Entry, $target.'/'.$entry);
			}

			$d->close();
		}else{
			copy($source, $target);
		}
	}

 	/**
	 * This method mimics a recursive delete in bash script
	 *
	 * Following syntax are recognized :	 
	 * 
	 * @param string $dir folder
	 * @return boolean validation
	 */
	public static function delTree($dir) {
		$files = array_diff(scandir($dir), array('.','..'));
		
		foreach ($files as $file) {
			(is_dir("$dir/$file")) ? static::delTree("$dir/$file") : unlink("$dir/$file");
		}
		
		return rmdir($dir);
	}
	
	/**
	 * This method removes files by specifics extensions from specifics folder(s).
	 * 
	 * Following syntax are recognized :
	 * 
	 * @param string $folder url (Example: folder/folder1/)
	 * @param array $types extensions	 
	 */
	public static function delFiles($folder, $types){
		foreach ($types as $type) {
			foreach(glob($folder."*.".$type) as $file){
				unlink($file);
			}
		}
	} 

}

?>